 
%include "ASMimplibs.sas";

%macro match_on_ppn_year(prod);

   data phy&prod;
    set db.phy&prod;
   run;

   proc sort data=phy&prod; by ppn year; run;

   data dbflags.phy&prod._with_flags in_phy&prod._not_in_flags;
    merge phy&prod (in=infhs) cmf_gs_flags (in=inflags);
     by ppn year;
    if infhs then output dbflags.phy&prod._with_flags;
    if infhs and inflags=0 then output in_phy&prod._not_in_flags;
   run;

   proc sort data=in_phy&prod._not_in_flags; by year; run;
   proc freq data= in_phy&prod._not_in_flags;
    tables ar;
   by year;
   title "Plants in FHS &prod dbase not matching to gs flags dataset";
   run;

%mend;



data cmf77flags (keep = ppn year ntvs_f_imp ncm_f_imp ncp_f_imp ncf_f_imp nee_f_imp nmh_f_imp npw_f_imp nww_f_imp nsw_f_imp nte_f_imp ntab_f_imp nmab_f_imp nbab_f_imp );
 set flags.cmf77_matched;
 year=1977;
run;

proc datasets library=work;
 modify cmf77flags;
 rename nmh_f_imp = nph_f_imp;
run;


data cmf82flags (keep = ppn year ntvs_f_imp ncm_f_imp ncp_f_imp ncf_f_imp nee_f_imp nph_f_imp npw_f_imp nww_f_imp nsw_f_imp nte_f_imp ntab_f_imp nmab_f_imp nbab_f_imp );
 set flags.cmf82_matched;
 year=1982;
run;


data cmf87flags (keep = ppn year tvs_f tib_F tie_f fib_f fie_f wib_f wie_f cm_f cp_f cf_f ee_f ph_f pw_f ww_f sw_f te_f tab_f tae_F mab_f mae_F bab_f bae_F );
 set flags.cmf87_matched;
 year=1987;
run;


data cmf92flags (keep = ppn year tvs_f tib_F tie_f fib_f fie_f wib_f wie_f cm_f cp_f cf_f ee_f ph_f ww_f pw_f sw_f te_f tab_f tae_F mab_f mae_F bab_f bae_F );
 set flags.cmf92_matched;
 year=1992;
run;


data cmf97flags (keep = ppn year tvs_f tib_F tie_f fib_f fie_f wib_f wie_f cm_f cp_f cf_f ee_f ph_f pw_f ww_f sw_f te_f tab_f tae_F );
 set flags.cmf97_matched;
 year=1997;
run;


/* Harmonize the impute indicators. */

data cmf87flags;
 set cmf87flags;
 if tvs_F = "I" then ntvs_f_imp=1;
 else ntvs_f_imp=0;
 if tib_F = "I" then ntib_f_imp=1;
 else ntib_f_imp=0;
 if tie_F = "I" then ntie_f_imp=1;
 else ntie_f_imp=0;
 if cm_F = "I" then ncm_f_imp=1;
 else ncm_f_imp=0;
 if cp_F = "I" then ncp_f_imp=1;
 else ncp_f_imp=0;
 if cf_F = "I" then ncf_f_imp=1;
 else ncf_f_imp=0;
 if ee_F = "I" then nee_f_imp=1;
 else nee_f_imp=0;
 if ph_F = "I" then nph_f_imp=1;
 else nph_f_imp=0;
 if pw_F = "I" then npw_f_imp=1;
 else npw_f_imp=0;
 if ww_F = "I" then nww_f_imp=1;
 else nww_f_imp=0;
 if te_F = "I" then nte_f_imp=1;
 else nte_f_imp=0;
 if tab_F = "I" then ntab_f_imp=1;
 else ntab_f_imp=0;
run;


data cmf92flags;
 set cmf92flags;
 if tvs_F = "I" then ntvs_f_imp=1;
 else ntvs_f_imp=0;
 if tib_F = "I" then ntib_f_imp=1;
 else ntib_f_imp=0;
 if tie_F = "I" then ntie_f_imp=1;
 else ntie_f_imp=0;
 if cm_F = "I" then ncm_f_imp=1;
 else ncm_f_imp=0;
 if cp_F = "I" then ncp_f_imp=1;
 else ncp_f_imp=0;
 if cf_F = "I" then ncf_f_imp=1;
 else ncf_f_imp=0;
 if ee_F = "I" then nee_f_imp=1;
 else nee_f_imp=0;
 if ph_F = "I" then nph_f_imp=1;
 else nph_f_imp=0;
 if pw_F = "I" then npw_f_imp=1;
 else npw_f_imp=0;
 if ww_F = "I" then nww_f_imp=1;
 else nww_f_imp=0;
 if te_F = "I" then nte_f_imp=1;
 else nte_f_imp=0;
 if tab_F = "I" then ntab_f_imp=1;
 else ntab_f_imp=0;
run;

data cmf97flags;
 set cmf97flags;
 ntvs_f_imp=0;
  ntib_f_imp=0;
  ntie_f_imp=0;
  ncm_f_imp=0;
  ncp_f_imp=0;
  ncf_f_imp=0;
  nee_f_imp=0;
  nph_f_imp=0;
  nww_f_imp=0;
  nww_f_imp=0;
  nte_f_imp=0;
  ntab_f_imp=0;
run;


data cmf_gs_flags;
 set cmf77flags
     cmf82flags
     cmf87flags     
     cmf92flags     
     cmf97flags;
run;

proc sort data=cmf_gs_flags; by ppn year; run;


%match_on_ppn_year(prod=boxf);
%match_on_ppn_year(prod=bredf);
%match_on_ppn_year(prod=carbonf);
%match_on_ppn_year(prod=cofff);
%match_on_ppn_year(prod=concf);
%match_on_ppn_year(prod=flrf);
%match_on_ppn_year(prod=gasf);
%match_on_ppn_year(prod=icebf);
%match_on_ppn_year(prod=icepf);
%match_on_ppn_year(prod=plyf);
%match_on_ppn_year(prod=sugf);



 

